package com.example.elmmerchants.Ringtone_Setting;

import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.IBinder;

import com.example.elmmerchants.R;

public class Get_Order_Ringtone extends Service {

    private MediaPlayer mediaPlayer;
    String which_order_type;
    AudioManager mAudio;

    public Get_Order_Ringtone() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        which_order_type = intent.getStringExtra("ringtone_name");
        if (which_order_type.equals("新订单")) {
            mediaPlayer = MediaPlayer.create(this, R.raw.new_order);
        }
        else if (which_order_type.equals("自动接单")) {
            mediaPlayer = MediaPlayer.create(this, R.raw.get_order_ringtone);
        }
        else if (which_order_type.equals("催单")) {
            mediaPlayer = MediaPlayer.create(this, R.raw.user_order_reminder);
        }
        else if (which_order_type.equals("订单被用户取消")) {
            mediaPlayer = MediaPlayer.create(this, R.raw.user_cancel_order);
        }
        else if (which_order_type.equals("用户申请退款")) {
            mediaPlayer = MediaPlayer.create(this, R.raw.user_refund);
        }
        else if (which_order_type.equals("配送异常")) {
            mediaPlayer = MediaPlayer.create(this, R.raw.abmormal_distribution);
        }
        if (!mediaPlayer.isPlaying()) {
            mediaPlayer.start();
        }
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
        }
        mediaPlayer.release();
    }
}